package com.dk.controller;

import com.dk.RES;
import com.dk.pojo.Tag;
import com.dk.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/api")
public class TagController {

    @Autowired
    TagService tagService;

    @GetMapping("/tags")
    @ResponseBody
    public RES getTags(){
        RES res=new RES();
        try{
            List<Tag> tags=tagService.getTags();
            res.setCode(0)
                    .setMsg("ok")
                    .addData("tags",tags);
        }catch (Exception e){
            e.printStackTrace();
            res.setCode(1).setMsg("error");
        }
        return res;
    }

    @PostMapping("/tag")
    @ResponseBody
    public RES addTag(@RequestParam("value") String value){
        RES res=new RES();
        try{
            Tag tag = tagService.addTag(value);
            res.setCode(0).setMsg("ok")
                    .addData("tag",tag);
        }catch (Exception e){
            e.printStackTrace();
            res.setCode(1).setMsg("error");
        }finally {
            return res;
        }
    }
}
